﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Deeper.Common;
using System.Text.RegularExpressions;

namespace Deeper.Engine
{
	public class Document
	{
		/// <summary>
		/// 参数
		/// </summary>
		public Dictionary<string, object> Items { private set; get; }

		public IList<IElement> Nodes;
		/// <summary>
		/// 模板文件
		/// </summary>
		public string TemplateFile { protected set; get; }

		public string TemplateContent { protected set; get; }

		public Document(string file)
		{
			Items = new Dictionary<string, object>();
			Nodes = new List<IElement>();
			LoadTemplate(file);
		}

		private void LoadTemplate(string file)
		{
			TemplateFile = file;
			TemplateContent = FileHelper.ReadTextFile(TemplateFile, Encoding.UTF8);
			ParserContent();
		}

		private void ParserContent()
		{
			if (TemplateContent.IsNull()) return;

			int charOffset = 0, offset = 0;
			//bool isClosedTag;
			Match match = null;

			while (offset < TemplateContent.Length)
			{
				if ((match = ProtetypeRegex.Match(TemplateContent, offset)).Success)
				{
					CreateTextNode(charOffset, match.Index - charOffset);
					Nodes.Add(new Variable(this, match.Groups["attr"].Value));
				}
				else if ((match = TagRegex.Match(TemplateContent, offset)).Success)
				{
					CreateTextNode(charOffset, match.Index - charOffset);

					string tagName = match.Groups["tag"].Value;
					Tag tag = TagFactory.CreateTag(this, tagName);
					tag.ProcessEndTag(ref match);
					Nodes.Add(tag);
				}
				//处理偏移位置
				if (match != null && match.Success)
				{
					charOffset = offset = match.Index + match.Length;
					match = null;
				}
				else
				{
					offset++;
				}
			}
			CreateTextNode(charOffset, TemplateContent.Length - charOffset);
		}

		public void CreateTextNode(int offset, int length)
		{
			if (length > 0)
			{
				string content = TemplateContent.Substring(offset, length);

				if (content.Length > 0)
				{
					//不建立空行文本节点
					if (content.TrimStart('\r', '\n', '\t').Length != 0)
						Nodes.Add(new TextNode(content));
				}
			}
		}

		private string ReplaceItems(string strSrc, string Tag)
		{
			//strSrc.
			return Tag;
		}
		/// <summary>
		/// 标签
		/// </summary>
		public static readonly Regex TagRegex = new Regex(@"{\$tag:(?<tag>[^\s]*)\s*item=\s*(?<list>[^\s]*)\s*}", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);
		/// <summary>
		/// 标签结束
		/// </summary>
		public static readonly Regex TagEndRegex = new Regex(@"{\$endtag:(?<tag>[^\s]*)\s*}", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);
		/// <summary>
		/// 标签内项
		/// </summary>
		public static readonly Regex EachItemRegex = new Regex(@"{\$eachItem:\s*(?<attr>[^\s]*)\s*}", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);
		/// <summary>
		/// 变量
		/// </summary>
		public static readonly Regex ProtetypeRegex = new Regex(@"{\$#:\s*(?<attr>[^\s]*)\s*}", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);

		public override string ToString()
		{
			String str = string.Empty;
			Nodes.ToList().ForEach(s => str += s.ToString());
			return str.ToString();
		}


		public string GetResponse()
		{
			string result = string.Empty;


			//Match match = ForeachRegex.Match(TemplateContent);

			//MatchCollection mc = ForeachRegex.Matches(TemplateContent);

			//mc.Cast<Match>().ToList().ForEach(s =>
			//{
			//    result += " <br />列表:" + s.Groups["list"].Value;
			//    result += " <br /> 内容:<br/>" + s.Groups["Content"].Value;
			//    ListItemRegex.Matches(s.Groups["Content"].Value).Cast<Match>().ToList().ForEach(p => result += p.Groups["attr"].Value);

			//    result += "列表结束";
			//});



			//Select(s => s.Groups["attr"])
			//match.Groups.Cast<Group>().ToList().ForEach(s => result += s.Value);
			return result;
		}
	}
}
